package gui.startUpWindow;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.LayoutStyle;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import javax.swing.border.LineBorder;

import core.CustomIcons;
import core.GuiComponentLabels;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class StartupWindow extends javax.swing.JFrame {
	private JButton closeButton;
	private JButton newTournamentButton;
	private JLabel logoLabel;
	private JButton openTournamentButton;
	private final StartupWindowController controller;
	private static StartupWindow instance = null;

	/**
	 * Auto-generated main method to display this JFrame
	 */
	public static void main(String [] args) {
		StartupWindowController.initDB();
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				StartupWindow inst = new StartupWindow();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}

	public static StartupWindow getInstance() {
		if (instance == null) {
			instance = new StartupWindow();
		}
		return instance;
	}

	private StartupWindow() {
		super();
		this.controller = new StartupWindowController();
		this.setUndecorated(true);
		this.initGUI();
	}

	private void initGUI() {
		this.setIconImage(CustomIcons.iconImage);
		try {
			GroupLayout thisLayout = new GroupLayout(this.getContentPane());
			this.getContentPane().setLayout(thisLayout);
			this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			{
				this.closeButton = new JButton();
				this.closeButton.setText(GuiComponentLabels.CLOSE_BUTTON);
				this.closeButton.setIcon(CustomIcons.exit);
				this.closeButton.setToolTipText("Beendet das Programm");
				this.closeButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						StartupWindow.this.dispose();
					}
				});
			}
			{
				this.logoLabel = new JLabel();
				this.logoLabel.setIcon(CustomIcons.logoBig);
				this.logoLabel.setBorder(new LineBorder(new java.awt.Color(0, 0, 0), 1, false));
			}
			{
				this.newTournamentButton = new JButton();
				this.newTournamentButton.setText(GuiComponentLabels.NEW_BUTTON);
				this.newTournamentButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						StartupWindow.this.controller.createNewTournament();
						StartupWindow.this.dispose();
					}
				});
			}
			{
				this.openTournamentButton = new JButton();
				this.openTournamentButton.setText(GuiComponentLabels.OPEN_BUTTON);
				this.openTournamentButton.setToolTipText("Öffnet ein bereits bestehendes Turnier");
				this.openTournamentButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						if (StartupWindow.this.controller.openTournament()) {
							StartupWindow.this.dispose();
						}
					}
				});
			}
			thisLayout.setVerticalGroup(thisLayout
					.createSequentialGroup()
					.addContainerGap()
					.addComponent(this.logoLabel, 0, 377, Short.MAX_VALUE)
					.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
					.addGroup(
							thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
									.addComponent(this.newTournamentButton, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 32, GroupLayout.PREFERRED_SIZE)
									.addComponent(this.openTournamentButton, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 32, GroupLayout.PREFERRED_SIZE)
									.addComponent(this.closeButton, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 32, GroupLayout.PREFERRED_SIZE)).addContainerGap());
			thisLayout.setHorizontalGroup(thisLayout
					.createSequentialGroup()
					.addContainerGap()
					.addGroup(
							thisLayout
									.createParallelGroup()
									.addGroup(
											GroupLayout.Alignment.LEADING,
											thisLayout.createSequentialGroup().addComponent(this.newTournamentButton, GroupLayout.PREFERRED_SIZE, 191, GroupLayout.PREFERRED_SIZE)
													.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
													.addComponent(this.openTournamentButton, GroupLayout.PREFERRED_SIZE, 226, GroupLayout.PREFERRED_SIZE)
													.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
													.addComponent(this.closeButton, GroupLayout.PREFERRED_SIZE, 191, GroupLayout.PREFERRED_SIZE).addGap(0, 0, Short.MAX_VALUE))
									.addComponent(this.logoLabel, GroupLayout.Alignment.LEADING, 0, 630, Short.MAX_VALUE)).addContainerGap());
			this.pack();
			this.setSize(654, 444);
		} catch (Exception e) {
			// add your error handling code here
			e.printStackTrace();
		}
	}

}
